<template>
  <el-upload
    class="upload-demo"
    action=""
    accept=".xlsx,.xls"
    :on-remove="handleRemove"
    :before-remove="beforeRemove"
    :on-change="handleChange"
    :limit="1"
    :file-list="fileList"
    :auto-upload="false"
    ref="upload"
  >
    <el-button size="small" type="primary">点击上传</el-button>
    <div slot="tip" class="el-upload__tip">
      只能上传excel文件
    </div>
  </el-upload>
</template>

<script>
export default {
  name: 'upload',
  props: {
    fileCallback: {
      type: Function,
      default: () => {}
    }
  },
  data() {
    return {
      fileList: [],
      importFile: ''
    }
  },
  methods: {
    clearFileList() {
      this.fileList = []
      this.$emit('fileCallback', false)
      this.$refs.upload.clearFiles()
    },
    handleChange(file, fileList) {
      this.fileList = fileList
      this.importFile = file
      this.$emit('fileCallback', file)
    },
    handleRemove(file, fileList) {
      this.importFile = file
      this.fileList = fileList
      this.$emit('fileCallback', false)
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`)
    }
  }
}
</script>
